local app = require 'app'
local game = app.game.running
local log = game.logging.get("seat")

local M = game.ui.element()

function M:ctor(local_no)
    self.res = "club.threecard.doudizhu/ui/ingame.b:seat"

    self.player = nil
    self.local_no = local_no
    
    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.avatar_icon = AvatarIcon.new()

    local bid_result = game.require 'ui.ingame.bid_result'
    self.bid_result = bid_result.new()

    local container = game.require 'pokers.poker_container'
    local poker_container_config = game.require 'pokers.poker_container_config'
    self.poker_container = container.new(poker_container_config[self.local_no])
end

function M:on_injected(bind)
    self.poker_container:set_gameobject(self.poker_container_obj, true)
end

function M:load(root)
    self.seat_root = root
    self:super("load")
end

function M:on_load()
    self:super("on_load")
    self.avatar_icon:load()
    self.avatar_icon:set_parent(self.head_root, false)
    self.bid_result:load()
    self.bid_result:hide()

    if self.should_set_data then
        self:on_set_data()
    end

    self:set_parent(self.seat_root, false)
    self.poker_container.hand_tran:SetParent(self.seat_root:Find('hand_root'), false)
    self.poker_container.lastchu_tran:SetParent(self.seat_root:Find('handout_root'), false)
    self.poker_container.ming_tran:SetParent(self.seat_root:Find('publish_root'), false)
    self.clock_root:SetParent(self.seat_root:Find('clock_root'), false)
    self.pass_flag.transform:SetParent(self.seat_root:Find('pass_root'), false)
    self.bid_result:set_parent(self.seat_root:Find('bid_result_root'), false)
end

function M:set_data(player)
    if not self.loaded then
        self.player = player
        self.should_set_data = true
        return
    end

    self.player = player
    self:on_set_data()
end

function M:on_set_data()
    self.should_set_data = false
    self.poker_container:clear()
    self:mark_pass(false)

    if self.player == nil then
        self.root.gameObject:SetActive(false)
        self.empty_root.gameObject:SetActive(true)
        return
    end

    self.root.gameObject:SetActive(true)
    self.empty_root.gameObject:SetActive(false)
    self:ready_change(self.player.is_ready==1)

    self.avatar_icon:set_head(self.player.head)
    self.name_txt.text = self.player.name
    self.chip_txt.text = self.player.chip
end

function M:ready_change(is_ready)
    self.ready_flag:SetActive(is_ready)
end

function M:on_handout()
    self.poker_container:clear_chupai()
    self:mark_pass(false)
end

function M:handout(card_ids)
    if #card_ids == 0 then
       self:mark_pass(true)
    else
        self:mark_pass(false)
        self.poker_container:chupai(card_ids)
    end
end

function M:mark_lord(b)
    self.lord_flag:SetActive(b)
end

function M:mark_pass(b)
    self.pass_flag:SetActive(b)
end

function M:on_release()
    if not self.loaded then
        return
    end

    self.poker_container_obj.transform:SetParent(self.transform, false)
end

function M:clear_scene()
    self.poker_container:clear()
    self:mark_pass(false)
    self:mark_lord(false)
    self.bid_result:hide()
    self:ready_change(false)
end

return M